﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.USB.HID;
using WFNetLib;
using System.Management;

namespace 脂肪仪
{
    public partial class Form1 : Form
    {
        IntPtr m_hUsbEventHandle;
        public zfyUSB TheHIDDevice = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_hUsbEventHandle = Win32Usb.RegisterForUsbEvents(Handle, Win32Usb.HIDGuid);
            if (TheHIDDevice == null)
            {
                TheHIDDevice = (zfyUSB)HIDDevice.FindDevice(0x10c4, 0x82cd, typeof(zfyUSB));
            }
            if (TheHIDDevice != null)
            {
                TheHIDDevice.CheckDevice();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string cpuInfo = "";//cpu序列号
            ManagementClass cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach(ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                //Response.Write ("cpu序列号："+cpuInfo.ToString ());
            }

            //获取硬盘ID
            String HDid;
            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();
            foreach(ManagementObject mo in moc1)
            {
                HDid = (string)mo.Properties["Model"].Value;
                //Response.Write ("硬盘序列号："+HDid.ToString ());
            }

            //获取网卡硬件地址
            //9558821702001755616
            string strmac;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc2 = mc.GetInstances();
            foreach(ManagementObject mo in moc2)
            {
                if ((bool)mo["IPEnabled"] == true)
                    strmac = mo["MacAddress"].ToString();
                //Response.Write("MAC address\t{0}"+mo["MacAddress"].ToString());
                mo.Dispose();
            }
            //主板
            string strbNumber = string.Empty;
            ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_baseboard");
            foreach (ManagementObject mo in mos.Get())
            {
                strbNumber=mo["SerialNumber"].ToString();
                break;
            }

        }
//         protected override void WndProc(ref Message m)
//         {
//             try
//             {
//                 if (m.Msg == 0x0219)
//                 {
//                     switch (m.WParam.ToInt32())
//                     {
//                         case 0x8000:
//                             if (TheHIDDevice == null)
//                             {
//                                 TheHIDDevice = (zfyUSB)HIDDevice.FindDevice(0x10c4, 0x82cd, typeof(zfyUSB));
//                             }
//                             break;
//                         case 0x8004:
//                             try
//                             {
//                                 TheHIDDevice.Dispose();
//                                 TheHIDDevice = null;
//                             }
//                             catch { }
//                             
//                             break;
//                         default:
//                             break;
//                     }
//                 }
//             }
//             catch (Exception ex)
//             {
//                 MessageBox.Show(ex.Message);
//             }
//             base.WndProc(ref m);
//         }

    }
}
